home *** CD-ROM | disk | FTP | other *** search
/ Hyper Stacks 1994 May / Hyper Stacks (Pacific HiTech)(1994)[Mac].iso / HyperTalk / Serial Port Toolkit v.2.5 / Source Code / recvChars.p < prev    next >
Encoding:
Text File  |  1988-04-18  |  2.2 KB  |  102 lines  |  [TEXT/MPS ]

  1. (*
  2.     recvChars(count) -- Return count characters from the serial port.
  3.  
  4.     To compile and link this file using Macintosh Programmer's Workshop,
  5.  
  6.         pascal -w recvChars.p
  7.         link -m ENTRYPOINT -o HyperCommands -rt XFCN=7031 -sn Main=recvChars ∂
  8.             recvChars.p.o "{MPW}"Libraries:interface.o
  9.  
  10.     © Copyright 1987,88 by Apple Computer, Inc.
  11.  
  12.     Initial coding 9/87 by Harry R. Chesley.
  13. *)
  14.  
  15. {$R-}
  16.  
  17. {$S recvChars }     { Segment name must be the same as the command name. }
  18.  
  19. unit DummyUnit;
  20.  
  21. interface
  22.  
  23. uses MemTypes, QuickDraw, OSIntf, HyperXCmd;
  24.  
  25. procedure EntryPoint(paramPtr: XCmdPtr);
  26.     
  27. implementation
  28.  
  29. type
  30.  
  31. Str31 = String[31];
  32.  
  33. procedure recvChars(paramPtr: XCmdPtr); forward;
  34.  
  35. procedure EntryPoint(paramPtr: XCmdPtr);
  36.  
  37.     begin
  38.         recvChars(paramPtr);
  39.     end;
  40.  
  41. procedure recvChars(paramPtr: XCmdPtr);
  42.  
  43.     var readCount: longInt;
  44.         readCountStr: Str255;
  45.         l: longInt;
  46.         p: Ptr;
  47.  
  48.     {$I XCmdGlue.inc}
  49.  
  50.     procedure Fail(errMsg: Str255); { set theResult and quit }
  51.         begin
  52.             paramPtr^.returnValue := PasToZero(errMsg);
  53.             exit(recvChars);
  54.         end;
  55.  
  56.     {$I SPortUtil.inc}
  57.  
  58.     begin
  59.         if paramPtr^.paramCount <> 1 then Fail('parameter count is not 1');
  60.  
  61.         ZeroToPas(paramPtr^.params[1]^,readCountStr);        { First parameter is count to read. }
  62.         readCount := StrToNum(readCountStr);
  63.         if readCount < 0 then Fail('invalid count');
  64.  
  65.         SetUpSPortGlobals;
  66.         EnsureOpenPort;
  67.  
  68.         { Create the input handle. }
  69.         paramPtr^.returnValue := NewHandle(readCount+1);
  70.         HLock(paramPtr^.returnValue);
  71.         { Read, read, read... }
  72.         if readCount > 0 then
  73.             begin
  74.                 l := readCount;
  75.                 if FSRead(ThisSPort.portInDev,l,paramPtr^.returnValue^) <> noErr then
  76.                     begin
  77.                         DisposHandle(paramPtr^.returnValue);
  78.                         Fail('FSRead failed');
  79.                     end;
  80.                 { Shrink the handle if we didn't get everything (paranoid programing; this should be impossible). }
  81.                 if l <> readCount then SetHandleSize(paramPtr^.returnValue,l+1);
  82.             end
  83.         else l := 0;
  84.         { Tack on a zero termination byte. }
  85.         p := ptr(ord4(paramPtr^.returnValue^)+l);
  86.         p^ := 0;
  87.         { Should we strip? }
  88.         if ThisSPort.stripIncoming then
  89.             begin
  90.                 { If so, rip dem bits off. }
  91.                 p := paramPtr^.returnValue^;
  92.                 while p^ <> 0 do
  93.                     begin
  94.                         p^ := BAND(p^,$7F);
  95.                         p := pointer(ord4(p)+1);
  96.                     end;
  97.             end;
  98.         HUnlock(paramPtr^.returnValue);
  99.     end;
  100.  
  101. end.
  102.